<template>
    <!-- 添加优惠券 -->
    <div class="addcoupon_form">
        <el-card>
            <el-form :model="form" label-width="120px" ref="ruleFormRef" :rules="rules" class="demo-ruleForm">

                <el-form-item label="优惠券类型:">
                    <el-select v-model="couponType" class="m-2" placeholder="Select" size="large">
                        <el-option v-for="item in couponTypes" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="优惠券名称:" prop="name">
                    <el-input v-model="form.name" />
                </el-form-item>
                <el-form-item label="适用平台:">
                    <el-select v-model="terrace" class="m-2" placeholder="Select" size="large">
                        <el-option v-for="item in terraces" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="总发行量:" prop="publishCount">
                    <el-input v-model="form.publishCount" placeholder="只能输入正整数" />
                </el-form-item>
                <el-form-item label="面额:" prop="amount">
                    <el-input v-model="form.amount" placeholder="面额只能是数值,限2位小数">
                        <template #append>元</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="每人限领:">
                    <el-input v-model="form.perLimit">
                        <template #append>张</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="使用门槛:" prop="minPoint">
                    <el-input v-model="form.minPoint" placeholder="只能输入正整数">
                        <template #prepend>满</template>
                        <template #append>元可用</template>
                    </el-input>
                </el-form-item>

                <el-form-item label="领取日期:">
                    <el-col :span="11">
                        <el-date-picker v-model="form.startTime" type="date" placeholder="选择日期" style="width: 100%" />
                    </el-col>
                </el-form-item>

                <el-form-item label="有效期:">
                    <el-col :span="11">
                        <el-date-picker v-model="form.enableTime" type="date" placeholder="选择日期" style="width: 100%" />
                    </el-col>
                    <el-col :span="2" class="text-center">
                        <span class="text-gray-500">至</span>
                    </el-col>
                    <el-col :span="11">
                        <el-date-picker v-model="form.endTime" type="date" placeholder="选择日期" style="width: 100%" />
                    </el-col>
                </el-form-item>

                <el-form-item label="可使用商品:">
                    <el-tabs v-model="activeName" type="card" class="demo-tabs" @tab-click="handleClick">
                        <el-tab-pane label="全场通用" name="second"></el-tab-pane>
                        <el-tab-pane label="指定分类" name="third">
                            <el-cascader v-model="value" :options="options" @change="handleChange" placeholder="请选择分类名称" />
                        </el-tab-pane>
                        <el-tab-pane label="指定商品" name="fourth">指定商品</el-tab-pane>
                    </el-tabs>
                </el-form-item>
                <el-form-item label="备注:">
                    <el-input v-model="form.note" type="textarea" placeholder="请输入内容" style="min-height: 32px;" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit(ruleFormRef, form)">提交</el-button>
                    <el-button @click="resettingFn">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>
  
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import type { TabsPaneContext } from 'element-plus'
// 导入请求接口
import * as api from '../../request/api'
// 导入类型
import type { ICreateCoupon } from '../../request/SmsAdminConreiller/index'
// 提示组件
import { ElTable } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

// 优惠券类型
const couponType = ref(0)
const couponTypes = [
    {
        value: 0,
        label: '全场赠券',
    },
    {
        value: 1,
        label: '会员赠券',
    },
    {
        value: 2,
        label: '购物赠券',
    },
    {
        value: 3,
        label: '注册赠券',
    }
]

// 平台类型
let terrace = ref(0)
const terraces = [
    {
        value: 0,
        label: '全平台',
    },
    {
        value: 1,
        label: '移动平台',
    },
    {
        value: 2,
        label: 'PC平台',
    }
]
// 
const ruleFormRef = ref<FormInstance>()


const options = [
    {
        value: 'guide',
        label: 'Guide',
        children: [
            {
                value: 'disciplines',
                label: 'Disciplines',

            },
        ],
    }]

// 可使用商品  
const activeName = ref('first')
const handleClick = (tab: TabsPaneContext, event: Event) => {
    console.log(tab, event)
}
// 请选择分类名称
const value = ref([])
const handleChange = (value: any) => {
    console.log(value)
}

// 表单数据
const form = reactive({
    amount: null,
    enableTime: '',
    endTime: '',
    minPoint: null,
    name: '',
    note: '',
    perLimit: null,
    platform: 0,
    productCategoryRelationList: [],
    productRelationList: [],
    startTime: '',
    publishCount: null,
    type: couponType.value,
    useType: terrace.value
})

// 重置
let resettingFn = () => {
    form.amount = null;
    form.enableTime = '';
    form.endTime = '';
    form.minPoint = null;
    form.name = '';
    form.note = '';
    form.perLimit = null;
    form.platform = 0;
    form.productCategoryRelationList = [];
    form.productRelationList = [];
    form.startTime = '';
    form.publishCount = null;
    form.type = 0;
    form.useType = 0;
}
// 提示
const rules = reactive<FormRules>({
    name: [
        { required: true, message: '请输入优惠券名称', trigger: 'blur' },
        { min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur' },
    ],
    publishCount: [
        { required: true, message: '只能输入正整数', trigger: 'blur' }
    ],
    amount: [
        { required: true, message: '面值只能是数值，0.01-10000，限2位小数', trigger: 'blur' }
    ],
    minPoint: [
        { required: true, message: '只能输入正整数', trigger: 'blur' }
    ]
})
// 发起请求添加数据
const onSubmit = async (formEl: FormInstance | undefined, form: ICreateCoupon) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            // console.log('submit!')
            // 二次 确认 
            ElMessageBox.confirm(
                '是否提交数据?', "提示",
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(async () => {
                    await api.flash.createCouponItem(form)
                    ElMessage({
                        type: 'success',
                        message: '提交成功',
                    })
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: '取消提交',
                    })
                })
        } else {
            ElMessage.error('验证失败')
            // console.log('error submit!', fields)
        }
    })
}
</script>
<style lang="scss" scoped>
.el-card {
    border-radius: 4px;
    border: 1px solid #ebeef5;
    background-color: #fff;
    overflow: hidden;
    color: #303133;
    transition: .3s;
}

.addcoupon_form {
    position: absolute;
    left: 0;
    right: 0;
    width: 720px;
    padding: 35px 35px 15px 35px;
    margin: 20px auto;
}

.demo-tabs>.el-tabs__content {
    padding: 32px;
    color: #6b778c;
    font-size: 32px;
    font-weight: 600;
}

.el-tabs--right .el-tabs__content,
.el-tabs--left .el-tabs__content {
    height: 100%;
}
</style>